﻿using System;
using System.Collections.Generic;
using System.IO;
using TpsAutoSurvey_PDA_Version_.RealizedClasses;
using System.Text;
using System.Collections;
using System.Threading;
using System.Reflection;
using TpsAutoSurvey_PDA_Version_.SocketModule;

namespace TpsAutoSurvey_PDA_Version_.Presenter
{
    public abstract class SurveyBase : SurveyInterface
    {
        private Thread thr;
        protected virtual void ManualSurvey() { }
        protected virtual void AutomaticSurvey() { }
        protected int curTimeNo;
        public PdaClient pdaClient { get; set; }
        #region SurveyInterface 成员
        public virtual event EventHandler OnerrHappen;
        public virtual event EventHandler OnSurveyComplete;
        public DataTransmission datatrans
        {
            get;
            set;
        }

        private bool isWorking = false;
        public bool IsWorking
        {
            get
            {
                return this.isWorking;

            }
            set
            {
                this.isWorking = value;
                OnWorking();

            }
        }

        protected  void OnWorking()
        {
            if (this.IsWorking == true)
            {
                this.pdaClient.IsWorking = true;
            }
            else
            {
                this.pdaClient.IsWorking = false;
            }
        }
        //发送到TCP Sever 服务器
        protected void SendToTcpServer(string header, string cmd, object para)
        {
            if (this.pdaClient == null) return;
            SendData sd = new SendData(header, cmd, para);
            this.pdaClient.SendJson(sd);
            Thread.Sleep(100);
        }
        public virtual Hashtable GetMethodList()
        {

            Hashtable hash = new Hashtable();
            Type t = typeof(SurveyInterface);
            foreach (MethodInfo mInfo in t.GetMethods())
            {
                object[] objAttrs = mInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objAttrs.Length > 0)
                {
                    DescriptionAttribute attr = objAttrs[0] as DescriptionAttribute;
                    if (attr != null)
                    {
                        hash.Add(attr.Description, mInfo.Name);
                    }
                }
            }
            return hash;
        }

        public CommandPara para
        {
            get;
            set;
        }

        public virtual CommandPara StartSurvey(CommandPara p)
        {
            try
            {
                if (this.IsWorking == false)
                {
                    para = p;
                }
                else
                {
                    p.result = "正在测量中";
                    return p;
                }
                thr = new Thread(new ThreadStart(ManualSurvey));
                thr.Start();
                p.result = "测量指令已下达";
                return p;
            }
            catch
            {
                this.isWorking = false;
                p.result = "发生错误!";
                return p;
            }
        }



        public virtual void StartSurvey()
        {
            try
            {
                if (this.IsWorking == false)
                {
                    para = new CommandPara("", "", "");
                    AutomaticSurvey();
                }
            }
            catch
            {
                this.IsWorking = false;
            }
        }

        public virtual CommandPara PrepareStation(CommandPara p)
        {
            p.result = "不支持的方法";
            return p;
        }

        public virtual void GetCurNo()
        {
            curTimeNo = this.datatrans.GetCurNo;
        }

        public virtual void PrepareStation()
        {
            PrepareStation(new CommandPara("", ""));
        }

        public virtual CommandPara StopSurvey(CommandPara p)
        {
            try
            {
                if (thr != null)
                {
                    thr.Abort();
                }
                IsWorking = false;
                return p;
            }
            catch
            {
                IsWorking = false;
                p.result = "停止失败";
                return p;
            }
        }

        public virtual void StopSurvey()
        {
            StopSurvey(new CommandPara("", ""));
        }

        public virtual CommandPara IsTPSConnected(CommandPara p)
        {
            p.result = "不支持的方法";
            return p;
        }

        public virtual CommandPara ShutDown(CommandPara p)
        {
            p.result = "不支持的方法";
            return p;
        }

        public virtual void ShutDown()
        {
            ShutDown(new CommandPara("", ""));
        }

        #endregion

        public SurveyBase()
        {
            this.pdaClient = new PdaClient();
            this.pdaClient.OnMessageCome = new PdaClient.MessageCome(MsgCome);
            this.pdaClient.OnSurveyTimeUp = new PdaClient.SurveyTimeUp(mytimer_SurveyTimeup);
        }
        public virtual void MsgCome(SendData data)
        {
            
        }
        public virtual void StartTimer()
        {
            this.pdaClient.EnableTimer = true;
        }
        public virtual void StopTimer()
        {
            this.pdaClient.EnableTimer = false;
        }
        void mytimer_SurveyTimeup()
        {
            try
            {
                if (this.IsWorking == true)
                {
                    return;
                }
                Thread thr = new Thread(new ThreadStart(StartSurvey));
                thr.Start();
            }
            catch
            {
                this.IsWorking = false;
                return;
            }
        }

    }
}
